package com.xydtech.workflow.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.workflow.domain.*;
import com.xydtech.workflow.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程版本信息Controller
 *
 * @author JHON
 * @date 2021-04-11
 */
@Controller
@RequestMapping("/workflow/flowversion")
public class WflFlowversionController extends BaseController {
    private String prefix = "workflow/flowversion";

    @Autowired
    private IWflFlowversionService wflFlowversionService;
    @Autowired
    private IWflFlownodeDefineService wflFlownodeDefineService;
    @Autowired
    private IWflFlowstepService wflFlowstepService;
    @Autowired
    private IWflSteppageService wflSteppageService;
    @Autowired
    IWflStepskipService wflStepskipService;
    @Autowired
    IWflSteplogicService wflSteplogicService;
    @Autowired
    IWflFlowdefineService wflFlowdefineService;
    @Autowired
    private IWflStepDocService wflStepDocService;

    @RequiresPermissions("workflow:flowdefine:view")
    @GetMapping()
    public String flowversion() {
        return prefix + "/flowversion";
    }

    /** 查询流程版本信息列表 */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WflFlowversion wflFlowversion) {
        startPage();
        List<WflFlowversion> list = wflFlowversionService.selectWflFlowversionList(wflFlowversion);
        return getDataTable(list);
    }

    /** 新增流程版本信息 */
    @RequiresPermissions("workflow:flowdefine:add")
    @Log(title = "流程版本信息", businessType = BusinessType.INSERT)
    @GetMapping("/add/{flowId}")
    public String add(@PathVariable("flowId") Long flowId, ModelMap mmap) {
        WflFlowversion wflFlowversion = new WflFlowversion();
        wflFlowversion.setFlowId(flowId);
        wflFlowversion.setFlowProgress("0");
        wflFlowversion.setUpTime(DateUtils.getTime());
        wflFlowversion.setUpUser(ShiroUtils.getUserId());
        wflFlowversion.setSts("0");
        wflFlowversionService.insertWflFlowversion(wflFlowversion);
        mmap.put("wflFlowversion", wflFlowversion);
        return prefix + "/flowconfig";
    }

    /** 修改流程版本信息 */
    @RequiresPermissions("workflow:flowdefine:edit")
    @GetMapping("/edit/{versionId}")
    public String edit(@PathVariable("versionId") Long versionId, ModelMap mmap) {
        WflFlowversion wflFlowversion = wflFlowversionService.selectWflFlowversionById(versionId);

        mmap.put("wflFlowversion", wflFlowversion);
        return prefix + "/flowconfig";
    }


    /** 删除流程版本信息 */
    @RequiresPermissions("workflow:flowdefine:remove")
    @Log(title = "流程版本信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wflFlowversionService.deleteWflFlowversionByIds(ids));
    }

    /** 查询流程节点未选步骤列表 */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/noChoiceList/")
    @ResponseBody
    public TableDataInfo noChoiceList(WflFlowversion wflFlowversion) {
        startPage();
        List<WflFlownodeDefine> list =
                wflFlownodeDefineService.selectNoChoiceFlownodeByVersionId(wflFlowversion.getVersionId());
        return getDataTable(list);
    }

    /** 查询流程节点已选步骤列表 */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/choiceList/")
    @ResponseBody
    public TableDataInfo choiceList(WflFlowversion wflFlowversion) {
        startPage();
        List<WflFlowstep> list = wflFlowstepService.selectWflFlowstepById(wflFlowversion.getVersionId());
        return getDataTable(list);
    }

    /**
     *检查当前配置步骤流程节点信息是否都已配置完成
     * @Title: checkConfigStep
     * @param versionId
     * @param configStepNum
     * @return com.xydtech.common.core.domain.AjaxResult
     * @exception
     * @author JHON
     * @Date 2021/6/1 19:12
     **/
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/checkconfigstep/")
    @ResponseBody
    public AjaxResult checkConfigStep(Long flowId, Long versionId, Long configStepNum) {
        try {
            //第一步节点配置
            if (configStepNum == 1) {

                List<WflFlowstep> stepList = wflFlowstepService.selectWflFlowstepById(versionId);
                if (stepList.size() < 2) {
                    return error("流程至少选择两个节点！");
                }
                //第二步审批配置
            } else if (configStepNum == 2) {
                WflFlowstep wflFlowstep = new WflFlowstep();
                wflFlowstep.setVersionId(versionId);
                wflFlowstep.setSts("0");
                List<WflFlowstep> stepList = wflFlowstepService.selectWflFlowstepList(wflFlowstep);
                if (stepList.size() > 0) {
                    return error("节点【" + stepList.get(0).getStepName() + "】未配置审批信息！");
                }
                //第三步节点页面配置
            } else if (configStepNum == 3) {
                /* 电子名片使用--去掉页面限制
                List<WflFlowstep> stepList = wflFlowstepService.selectWflFlowstepById(versionId);
                if (stepList.size() > 0) {
                    for (int i = 0; i < stepList.size(); i++) {
                        WflSteppage wflSteppage = new WflSteppage();
                        wflSteppage.setVersionId(stepList.get(i).getVersionId());
                        wflSteppage.setStepId(stepList.get(i).getStepId());
                        //查询流程节点页面配置信息
                        List<WflSteppage> stepPageList = wflSteppageService.selectWflSteppageList(wflSteppage);
                        if (stepPageList.size() == 0) {
                            return error("节点【" + stepList.get(i).getStepName() + "】未配置页面信息！");
                        }
                    }

                }*/
            } else if (configStepNum == 6) {
                //第六步配置完成更新流程最新版本
                //查询最新版本号
                WflFlowversion maxWflFlowversion = new WflFlowversion();
                maxWflFlowversion.setFlowId(flowId);
                List<WflFlowversion> list = wflFlowversionService.selectWflFlowversionList(maxWflFlowversion);
                //更新流程最新版本
                WflFlowdefine wflFlowdefine = new WflFlowdefine();
                wflFlowdefine.setFlowId(flowId);
                wflFlowdefine.setNewVersion(list.get(0).getVersionId());
                wflFlowdefineService.updateWflFlowdefine(wflFlowdefine);
            }
            //更新流程版本配置进度
            WflFlowversion wflFlowversion = new WflFlowversion();
            wflFlowversion.setVersionId(versionId);
            wflFlowversion.setFlowProgress(configStepNum.toString());
            wflFlowversionService.updateWflFlowversion(wflFlowversion);
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 查询流程节点页面配置列表
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/pageList")
    @ResponseBody
    public TableDataInfo pageList(WflFlowstep wflFlowstep) {
        //根据版本号和序号查询流程节点信息
        wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(wflFlowstep.getVersionId(), wflFlowstep.getOrderNum());
        List<WflSteppage> list = new ArrayList<>();
        if (wflFlowstep != null) {
            WflSteppage wflSteppage = new WflSteppage();
            wflSteppage.setVersionId(wflFlowstep.getVersionId());
            wflSteppage.setStepId(wflFlowstep.getStepId());
            //查询流程节点页面配置信息
            startPage();
            list = wflSteppageService.selectWflSteppageList(wflSteppage);
        }

        return getDataTable(list);
    }

    /**
     * 查询流程节点跳转配置列表
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/skipList")
    @ResponseBody
    public TableDataInfo skipList(WflFlowstep wflFlowstep) {
        //根据版本号和序号查询流程节点信息
        wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(wflFlowstep.getVersionId(), wflFlowstep.getOrderNum());
        List<WflStepskip> list = new ArrayList<>();
        if (wflFlowstep != null) {
            WflStepskip wflStepskip = new WflStepskip();
            wflStepskip.setVersionId(wflFlowstep.getVersionId());
            wflStepskip.setStepId(wflFlowstep.getStepId());
            //查询流程节点跳转配置信息
            startPage();
            list = wflStepskipService.selectWflStepskipList(wflStepskip);
        }

        return getDataTable(list);
    }

    /**
     *跳转选择页面模板页面并排除已选的页面ID
     * @Title: checkPage
     * @param versionId
     * @param orderNum
     * @param mmap
     * @return java.lang.String
     * @exception
     * @author JHON
     * @Date 2021/6/4 15:24
     **/
    @RequiresPermissions("workflow:flowdefine:view")
    @GetMapping("/checkPage/{versionId}/{orderNum}")
    public String checkPage(@PathVariable("versionId") Long versionId, @PathVariable("orderNum") Long orderNum, ModelMap mmap) {
        //根据版本号和序号查询流程节点信息
        WflFlowstep wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(versionId, orderNum);
        //查询已配置页面信息
        WflSteppage oldWflSteppage = new WflSteppage();
        oldWflSteppage.setStepId(wflFlowstep.getStepId());
        oldWflSteppage.setVersionId(versionId);
        List<WflSteppage> oldPageList = wflSteppageService.selectWflSteppageList(oldWflSteppage);
        String pageId = "";
        if (oldPageList.size() > 0) {
            //加工已当前流程节点选择的页面模板编号
            for (int i = 0; i < oldPageList.size(); i++) {
                pageId += oldPageList.get(i).getPageId() + ",";
            }
            pageId = pageId.substring(0, pageId.length() - 1);
        }
        mmap.put("pageIds", pageId);
        return prefix + "/checkpage";
    }

    /**
     * 查询流程节点逻辑配置列表
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/logicList")
    @ResponseBody
    public TableDataInfo logicList(WflFlowstep wflFlowstep) {
        //根据版本号和序号查询流程节点信息
        wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(wflFlowstep.getVersionId(), wflFlowstep.getOrderNum());
        List<WflSteplogic> list = new ArrayList<>();
        if (wflFlowstep != null) {
            WflSteplogic wflSteplogic = new WflSteplogic();
            wflSteplogic.setVersionId(wflFlowstep.getVersionId());
            wflSteplogic.setStepId(wflFlowstep.getStepId());
            //查询流程节点逻辑配置信息
            startPage();
            list = wflSteplogicService.selectWflSteplogicList(wflSteplogic);
        }

        return getDataTable(list);
    }

    /**
     * 修改流程版本状态
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "修改流程版本状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeSts")
    @ResponseBody
    public AjaxResult changeSts(WflFlowversion wflFlowversion) {
        //更新流程定义表启用版本号及配置状态
        WflFlowdefine wflFlowdefine = new WflFlowdefine();
        wflFlowdefine.setCurreVersion(wflFlowversion.getVersionId());
        wflFlowdefine.setFlowId(wflFlowversion.getFlowId());
        wflFlowdefine.setFlowProgress(wflFlowversion.getSts());
        wflFlowdefineService.updateWflFlowdefine(wflFlowdefine);
        //如果是启用则把其他版本改为停用
        if ("1".equals(wflFlowversion.getSts())) {
            WflFlowversion version = new WflFlowversion();
            version.setFlowId(wflFlowversion.getFlowId());
            version.setSts("0");
            wflFlowversionService.updateWflFlowVersionByFlowId(version);
        }
        //更新当前版本状态
        return toAjax(wflFlowversionService.updateWflFlowversion(wflFlowversion));
    }

    /**
     * 查询流程节点影像资料配置列表
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/docList")
    @ResponseBody
    public TableDataInfo docList(WflFlowstep wflFlowstep) {
        //根据版本号和序号查询流程节点信息
        wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(wflFlowstep.getVersionId(), wflFlowstep.getOrderNum());
        List<WflStepDoc> list = new ArrayList<>();
        if (wflFlowstep != null) {
            WflStepDoc wflStepDoc = new WflStepDoc();
            wflStepDoc.setVersionId(wflFlowstep.getVersionId());
            wflStepDoc.setStepId(wflFlowstep.getStepId());
            //查询流程节点资料上传配置信息
            startPage();
            list = wflStepDocService.selectWflStepDocList(wflStepDoc);
        }
        return getDataTable(list);
    }

    @GetMapping("/checkDocList/{versionId}/{orderNum}")
    public String checkDocList(@PathVariable("versionId") Long versionId, @PathVariable("orderNum") Long orderNum, ModelMap mmap) {
        //根据版本号和序号查询流程节点信息
        WflFlowstep wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(versionId, orderNum);
        //查询已配置影像信息
        WflStepDoc oldWflStepDoc = new WflStepDoc();
        oldWflStepDoc.setStepId(wflFlowstep.getStepId());
        oldWflStepDoc.setVersionId(versionId);
        List<WflStepDoc> oldStepDocList = wflStepDocService.selectWflStepDocList(oldWflStepDoc);
        String docId = "";
        if (oldStepDocList.size() > 0) {
            //加工当前流程节点选择的页面模板编号
            for (int i = 0; i < oldStepDocList.size(); i++) {
                docId += oldStepDocList.get(i).getDocId() + ",";
            }
            docId = docId.substring(0, docId.length() - 1);
        }
        mmap.put("docIds", docId);
        return prefix + "/checkDocList";
    }

    /**
     * 复制流程版本
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "复制流程版本", businessType = BusinessType.UPDATE)
    @PostMapping("/copy")
    @ResponseBody
    public AjaxResult copy(WflFlowversion wflFlowversion) {

        return toAjax(wflFlowversionService.copyWflFlowVersion(wflFlowversion));
    }
}
